package com.svms.service.util;

import com.svms.service.dml.iface.NumberGeneratorDMLIF;
import com.svms.service.factory.SvmsServiceFactory;

/**
 * @author om39a
 * 
 *         This class is used to generate next number for the data being pushed
 *         in to DB.
 * 
 */
public class NumberGeneratorUtils {

	private NumberGeneratorUtils() {

	}

	private static NumberGeneratorDMLIF numberGenerator = SvmsServiceFactory
			.getInstance().getNumberGenerator();

	public static long generateVehicleId() throws Exception {
		int nextNumber = numberGenerator.generateVehicleId();
		if (nextNumber != 0) {
			numberGenerator.updateNextVehicleId(nextNumber + 1);
		} else {
			System.out
					.println("~~~~~~~ Number generator Compomnent Error~~~~~~~");
			throw new Exception("Number generator Compomnent Error");
		}
		return nextNumber;
	}

	public static long generateMovemenrId() throws Exception {
		int nextNumber = numberGenerator.generateMovemenrId();
		if (nextNumber != 0) {
			numberGenerator.updateNextMovementId(nextNumber + 1);
		} else {
			System.out
					.println("~~~~~~~ Number generator Compomnent Error~~~~~~~");
			throw new Exception("Number generator Compomnent Error");
		}
		return nextNumber;
	}

	public static long generateTransportId() throws Exception {
		int nextNumber = numberGenerator.generateTransportId();

		if (nextNumber != 0) {
			numberGenerator.updateNextTransportId(nextNumber + 1);
		} else {
			System.out
					.println("~~~~~~~ Number generator Compomnent Error~~~~~~~");
			throw new Exception("Number generator Compomnent Error");
		}
		return nextNumber;
	}

	public static long generateIndentId() throws Exception {
		int nextNumber = numberGenerator.generateIndentId();

		if (nextNumber != 0) {
			numberGenerator.updateNextIndentId(nextNumber + 1);
		} else {
			System.out
					.println("~~~~~~~ Number generator Compomnent Error~~~~~~~");
			throw new Exception("Number generator Compomnent Error");
		}
		return nextNumber;
	}

	public static long generateSpareCodeId() throws Exception {
		int nextNumber = numberGenerator.generateSpareCodeId();

		if (nextNumber != 0) {
			numberGenerator.updateNextSpareCodeId(nextNumber + 1);
		} else {
			System.out
					.println("~~~~~~~ Number generator Compomnent Error~~~~~~~");
			throw new Exception("Number generator Compomnent Error");
		}
		return nextNumber;
	}

	public static long generateVechielSpareID() throws Exception {
		int nextNumber = numberGenerator.generateVechielSpareId();

		if (nextNumber != 0) {
			numberGenerator.updateNextVechielSpareId(nextNumber + 1);
		} else {
			System.out
					.println("~~~~~~~ Number generator Compomnent Error~~~~~~~");
			throw new Exception("Number generator Compomnent Error");
		}
		return nextNumber;
	}
}
